from functools import wraps
from flask import jsonify, request
from flask_jwt_extended import get_jwt, verify_jwt_in_request


def admin_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        # 先处理 OPTIONS 请求
        if request.method == 'OPTIONS':
            return jsonify({"code": 0, "message": "OPTIONS请求成功"})

        try:
            # 验证JWT
            verify_jwt_in_request()

            # 获取当前用户信息
            claims = get_jwt()

            # 检查用户角色是否为管理员
            # if not claims or not claims.get('role') == 'super_admin':
            #     return jsonify({
            #         "code": 403,
            #         "message": "权限不足，需要管理员权限"
            #     }), 403

        except Exception as e:
            print(e)
            # JWT验证失败
            return jsonify({
                "code": 401,
                "message": "未授权，请先登录"
            }), 401

        # 继续执行被装饰的函数
        return f(*args, **kwargs)

    return decorated_function